/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.gp.patient.consultation;

import be.ugent.medirem.logic.GpRemote;
import be.ugent.medirem.persistence.entity.values.ConsultationValues;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Simon
 */
public class AddPatientConsultationServlet extends HttpServlet {

    @EJB
    private GpRemote gpBean;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long pid = (Long) request.getSession().getAttribute("pid");
        request.setAttribute("patientid", pid);
        request.getRequestDispatcher("/gp/patient/consultation/add.jsp").forward(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long pid = (Long) request.getSession().getAttribute("pid");
        request.setAttribute("patientid", pid);
        String username = request.getUserPrincipal().getName();
        Long id = gpBean.getGpId(username);
        boolean validate = true;

        ConsultationValues c = (ConsultationValues) request.getAttribute("consultation");
        c.setGpId(id);
        try {
            gpBean.makeConsultation(c);
        } catch (EntityNotFoundException ex) {
            Logger.getLogger(AddPatientConsultationServlet.class.getName()).log(Level.SEVERE, null, ex);
            request.getSession().setAttribute("flash", "An error occured when making this consultation: cannot find patient or gp!");
            validate = false;
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(AddPatientConsultationServlet.class.getName()).log(Level.SEVERE, null, ex);
            request.getSession().setAttribute("flash", "An error occured when making this consultation: you already have a consultation at this time!");
            validate = false;
        } catch (Exception ex) {
            Logger.getLogger(AddPatientConsultationServlet.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("flash", "An error occured when making this consultation");
            validate = false;
        }

        if (validate) {
            request.getSession().setAttribute("flash", "Consultation added");
            response.sendRedirect(request.getContextPath() + "/gp/patient/main");
        } else {
            if (c != null)
                request.setAttribute("consultation", c);
            request.getRequestDispatcher("/gp/patient/consultation/add.jsp").forward(request, response);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
